from django.db import models
from .enums import ServiceCate, DockerRepoTypeChoices
from apps.system.models import User, Role
from apps.common.fields import EncryptCharField, EncryptTextField

class DockerRepo(models.Model):
    name = models.CharField(max_length=25, verbose_name='仓库名', unique=True)
    type = models.CharField(max_length=25, choices=DockerRepoTypeChoices, verbose_name='仓库类型', null=True)
    instance = models.CharField(max_length=255, verbose_name='实例ID', null=True, default=None)
    access_key_id = models.CharField(max_length=255, verbose_name='用户key', null=True, default=None)
    # access_key_secret = models.CharField(max_length=255, verbose_name='用户秘钥', null=True, default=None)
    access_key_secret = EncryptCharField(max_length=255, verbose_name='用户秘钥', null=True, default=None)
    url = models.CharField(max_length=255, verbose_name='仓库地址')
    username = models.CharField(max_length=25, verbose_name='用户名', null=True, default=None)
    # password = models.CharField(max_length=255, verbose_name='密码', null=True, default=None)
    password = EncryptCharField(max_length=255, verbose_name='密码', null=True, default=None)

    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name



class Kubernetes(models.Model):
    name = models.CharField(max_length=25, verbose_name='集群名', unique=True)
    # kubeconfig = models.TextField(verbose_name='集群密钥')
    kubeconfig = EncryptTextField(verbose_name='集群密钥')

    
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)


    def __str__(self):
        return self.name



class Business(models.Model):
    code = models.CharField(max_length=25, verbose_name='编码', unique=True)
    name = models.CharField(max_length=25, verbose_name='名称')
    
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name

class Project(models.Model):
    code = models.CharField(max_length=25, verbose_name='编码', unique=True)
    name = models.CharField(max_length=25, verbose_name='名称')
    business = models.ForeignKey(Business, related_name='projects', on_delete=models.CASCADE, verbose_name='业务线')
    git_url = models.URLField(max_length=255, verbose_name='Git地址', null=True, blank=True)
    git_master_branch = models.CharField(max_length=25, verbose_name='Git主分支', null=True, blank=True)

    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name

class Service(models.Model):
    code = models.CharField(max_length=64, verbose_name='编码', unique=True)
    name = models.CharField(max_length=64, verbose_name='名称')
    business = models.ForeignKey(Business, on_delete=models.CASCADE, verbose_name='业务线', related_name='services')
    project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='项目', related_name='services')
    cate = models.CharField(max_length=25, choices=ServiceCate, default=None, null=True, verbose_name='类别')
    git_url = models.URLField(max_length=255, verbose_name='Git地址', null=True, blank=True)
    git_master_branch = models.CharField(max_length=25, verbose_name='Git主分支', null=True, blank=True)


    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name

    # def business(self):
    #     self.project.business



class LobPerm(models.Model):
    user = models.ForeignKey(User, verbose_name='用户', on_delete=models.CASCADE, related_name='perms')
    business = models.ForeignKey(Business, verbose_name='业务线', on_delete=models.CASCADE, related_name='perms')
    project = models.ForeignKey(Project, verbose_name='项目', on_delete=models.CASCADE, null=True, default=None)
    service = models.ForeignKey(Service, verbose_name='服务', on_delete=models.CASCADE, null=True, default=None)

    roles = models.ManyToManyField(Role, verbose_name='角色')

    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['user', 'business', 'project', 'service'], name='用户业务唯一属性')
        ]
class Environment(models.Model):
    code = models.CharField(max_length=25, verbose_name='编码', unique=True)
    name = models.CharField(max_length=25, verbose_name='名称')
    cicd = models.CharField(choices=(('ci', 'CI'), ('cd', 'CD')), max_length=25, verbose_name='CICD')
    # audit = models.IntegerField(default=1, verbose_name='审核')
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    comment = models.CharField(max_length=255, verbose_name='备注', null=True, blank=True)

    def __str__(self):
        return self.name